This introduction to Smile is based upon an example. Together, we shall develop a small project, an applet. An applet is a small application made with AppleScript.
The applet that we shall design will make on the desktop a copy of the preferences file of the Energy Saver control panel. This is a way to keep a copy of the settings of the computer concerning automatic startup and shut down. Later, you may move such a saved preferences file back into the Preferences folder to restore previous settings.
The present document assumes that you have already installed properly Smile, as described in the Read me file. If this is not the case, please open the Read me first.
2. Where to find help
The Apple Guide help provides a basic help to get started with AppleScript. To open it, pull down the "Help" menu in the Finder, choose "Help center", and click AppleScript help.
Sooner or later, you will gain productivity by having handy the official documentation on AppleScript and the standard scripting additions, which can be found on the AppleScript site <http://www.apple.com/applescript>.
If Smile has been installed, Smile help is available. Like AppleScript help, it is available from the "Help center". When you are in Smile, just press the Help key, or its keyboard equivalent ?, or choose "Smile help" in the "Help" menu, to open it.
Smile help is searchable and browsable, use it for all details on the topics addressed below. The present document is a plain text file with pictures included, it contains no link of any kind.
3. Setting up one's environment
Let us get started with the project. If Smile is not already running, double-click the icon of Smile. If you have not modified the corresponding setting in the Preferences dialog of Smile, the "Worksheet" will open automatically.
The "Worksheet" is just a text document which is saved automatically when Smile quits. You will use it to store frequently needed material such as URL's, pictures or short scripts. Close it, or don't close it - anyhow it will re-open automatically if needed.
From the very beginning of the project, the experience of Smile is different from other editors. We intend to build an applet so, like in Script Editor, we will write the script in a "script window", and then save it as an applet.
But what is particular with Smile is that we will make the "script window" only as a final step. We will first build the script entirely with text windows only.
Choose "New text" in the "File" menu (or press N). The new empty "untitled" window will be where you will develop your script.
In the process of making the script, you will run short pieces of script from the "untitled" window. By default, the results would be appended at the end of the window. Instead, it is more convenient to have them fill an output window.
Pull down the "Scripts" menu (the menu with the parchment icon). The first section includes the most frequently used scripting helpers.
Choose "Output window" (or press L). This will bring a new, empty, text window, named "untitled. out", just below the "untitled" window.
To check how it works, type (for instance) :
in the "untitled" window, then press the Enter key () (or ctrl-C). NOT the return key (). Smile compiles the line, so that the style of the text changes (according to the current AppleScript format settings of your machine).
Simultaneously, the result (the current date) is appended to the output window.
Now, erase the contents of both windows, and let's get started with scripting.
4. Writing one's script
The specification of your project is the following : make an applet which will duplicate the preferences file of the Energy Saver control panel onto the Desktop.
Basically, the script must first build a reference to the file, and then use this reference to duplicate the file and put the copy on the desktop.
In the framework of the Guided tour, we will suppose that the name of this preferences file is fixed and will not change.
To build a full reference to it, you need to retrieve its full path. The file is located in the Preferences folder of the System Folder so, first thing, your script will have to get the path to the preferences folder. Of course, you could hardcode the path to it, but it will be safer - and simpler - to use a predefined system keyword.
Having quickly browsed Apple's documentation about the standard scripting additions, you know that a 'path to' command exists.
The problem is, what is its syntax exactly ? Is it "path to preferences folder" ? "path to the prefs" ? "path to preferences" ?
Let us check. Write "path to" in the "untitled" window. Hilite these two words by selecting them.
Pull down the "Edit" menu. The last section but one is particularly devoted to scripting.
Choose "Find definition" (or press F). The dictionary of the "scripting addition" containing the 'path to' command opens.
You observe that the keyword is "preferences". Select "preferences". Copy it, close the dictionary and paste it after "path to". Let us use a variable to store the path of the Preferences folder :
Next step, let us append the name of the file to the variable "thePath" and into a new variable.
Oh oh. Two things are embarrassing us. First thing, will 'path to' return a colon at the end of the string, or not ? No problem. Click in the first line, then press 'Enter'.
Smile runs the first line, and the "untitled.out" window displays the result, the contents of thePath.
It ends with a colon.
Second thing, what is the name of the file exactly ? No other way : you have to look in the Preferences folder. It's so boring to switch to Finder, open the disk, open the system folder, scroll as to see the preferences folder ... The path to the preferences folder is already stored in a variable, so it is easier to write two short lines to open it. Write them in the same "untitled" window.
Select the two lines and hit the Enter key. The Preferences folder opens in the Finder. You will quickly find a file named "Energy Saver Preferences". You can now erase the lines for the Finder - possibly after having copied them to the Worksheet for future use. Once finished, the second line of your script looks like :
A chinese proverb states "it is wise not to duplicate things before they exist". So, let us check that the file exists before we duplicate it.
There are many ways for testing for the existence of a file. The "alias" type of AppleScript (not to be confused with the "alias file" type of the Finder) is by definition the type of the existing files. Trying to make an alias out of a non-existing file will result in an error, that we can catch to display an appropriate dialog box.
Same problem than above. You remember that there exists a 'display dialog' command, but are you sure with its syntax ? Rather let Smile do it. Like you have done before, you could use the "Find definition" feature, to display the dictionary where it belongs. Smile makes it easier. Select the two words, and use the first choice in the "Scripts menu" …
… the "Balance" command (shortcut E). Smile writes the prototype of the syntax for you.
The types of the parameters are suggested by the variable names used by Smile. Replace each relevant parameter by its value, suppress the irrelevant ones, and here you are : you can be sure that your script line is syntaxically correct.
You have terminated the "on error" handler with "return", so that the script will quit after the user will have clicked "OK". Before you go further, it is a good thing to test the dialog. It is so hard to imagine the feeling a dialog provides to the user without running it. Put the cursor in the "display dialog" line, then press the Enter key.
All right. Note that, as usual, the result of the script you have run is appended to the "untitled.out" window.
Now we have a valid path name, we can duplicate the file. Moving files, renaming, deleting, duplicating them, is the task of the Finder. If you do not know how to have the Finder do it, you have to open the Finder's dictionary and browse it. You can open the dictionary via the corresponding item of the "File" menu.
But, if an application is running (which is always the case of Finder) it is more handy to use the "tell …" unique Smile's feature to open its dictionary. Check that the "untitled" window is the front window. Pull down the "Edit" menu and choose "tell …".
In the "Choose application" dialog box, choose "Finder", and click "OK". Note that the "untitled" window now has a menu entitled "Finder" in the bottom bar.
Choose "Dictionary". This opens the dictionary of the Finder.
Note that the dictionary also has a menu in its bottom bar, entitled "Index".
Pull down the "Index" menu. What you are looking for is the proper verb to duplicate a file. Verbs are in plain text, there are few of them. Quickly you will find "duplicate".
Choose it in the menu.
Copy "duplicate" to the "untitled" window (or use drag and drop if you prefer). Write a "tell" block around it :
Like you have done before, in order to get the proper syntax for "duplicate", select it in the "untitled" window, and choose "Balance" in the "Scripts" menu (or type E).
Smile writes for you :
"MyReference" obviously refers to the original file, i.e. "alias theFilePath". "MyLocation" should then refer to the desktop, just replace it with "desktop". Like all "global" objects, "desktop" is not in itself an entry in the Finder's dictionary. You will find it as a property of the "application" class - the only member of this class being the Finder in this context.
Whether or not to "replace" an already existing file with the same name is a matter of choice. In the frame of the Guided tour, we will chose to suppress any existing file with the same name. After having suppressed the irrelevant "routing suppressed" parameter (see in the dictionary what it is intended for), we get :
Last thing before you convert these lines into an applet, let us test your script from the first to the last line. First thing, break the link of the window to the Finder. If you let the window linked to the Finder, it will not be able to access Smile's variables, such as "thePath" that we have used already. This is because applications cannot share AppleScript variables - for obvious reasons. To suppress the link to the Finder, choose "Log out" in the "Finder" menu of the bottom bar.
The first line has run already and - unless you have quit Smile in the meanwhile - the variable "thePath" still exists. You can check its contents : select any occurrence of "thePath" in the script,
then press Enter. The result of the execution of the (short) script "thePath", which is simply the contents of this variable, is appended to the result window.
Now, click anywhere in the line 2 of your script "set theFilePath …" and press Enter. Do the same thing with line 4 "alias …". Then, select the whole "tell" block (3 lines) and press Enter. Each time you have pressed Enter the corresponding result was appended to the "untitled.out" window. The result of the latest execution is simply the reference to the new file, on the desktop.
The script runs just fine, now let us make it into an applet.
5. Making one's script into an applet
It is at this stage that you shall use a "script window" à la Script Editor. In the "File" menu, choose "New script".
A new empty, colored, "untitled 1" window opens. This is what is called a "script window".
Its color may differ, depending on the settings in the Preferences dialog ("Edit" menu).
Copy and paste your script from the white window to the colored window (or, use drag and drop). Note the buttons in the bottom bar.
"C" - for "Comment pane". Clicking this button will make the comment pane of the script visible. In the comment pane, you can store text, sounds, and pictures. For instance, you can store copyright and release infos, or your photo - if you think it is worth storing.
Launches the script (shortcut R) (same as choosing "Run" in the "Edit" menu).
Compiles the script (shortcut 'Enter' key) (same as choosing "Check syntax" in the "Edit" menu).
Displays the list of the handlers present in the script (yours has none).
You can still test your script. Click the arrow button . The script compiles, then runs. A new copy of the "Energy Saver Preferences" gets copied to the desktop, suppressing the previous copy. The result (the reference to the new file) is appended to the Worksheet, which is the default output window of all the script windows.
Everything seems all right, so let us save the script. Go in the "File" menu, choose "Save".
In the "Save" dialog, make sure you choose "Application" as the "Format", otherwise the script will be saved as a "Smile document", this meaning a mere script.
(Below, we have chosen to name the applet "Save ES prefs to desktop".)
Your applet is now stored at the destination you chose. You may close the windows.
In Finder, double-click the newly created applet.
A new copy of the Preference file of Energy Saver is created on the desktop, suppressing the latest one.
Congratulations. Your job is done. Before you quit the tour, you may want to have again a look at the section 2., about how to get helped. Smile Help will guide you as you start scripting with Smile, and it will be your reference when you go further with Smile.
To jump to the section 2., click anywhere in the line below, and press 'Enter'. (If there is no cursor, do not worry, this document may be locked. The click - then - 'Enter' still works.)
select (first paragraph of window 1 where it starts with "2."):